/*
 * JavaSEHttpConnection.java
 *
 * Created on 07/10/‎24 +0900
 *
 * $Revision: 45 $
 * $Date: 2008-10-26 17:16:20 +0000 (Sun, 26 Oct 2008) $
 */

package org.gikolet.framework.platform.javase.internal;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.gikolet.framework.io.http.HttpConnection;
import org.gikolet.framework.util.Assert;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 45 $ $Date: 2008-10-26 17:16:20 +0000 (Sun, 26 Oct 2008) $
 */
public class JavaSEHttpConnection implements HttpConnection {
    private URL url;

    private boolean close;
    private boolean connect;
    private HttpURLConnection httpURLConnection;

    public JavaSEHttpConnection(String url) throws IOException {
	Assert.isNotNull(url);

	this.url = new URL(url);

	this.httpURLConnection = (HttpURLConnection) this.url.openConnection();
	this.httpURLConnection.setRequestMethod(GET);

	this.close = false;
	this.connect = false;
    }

    public String getURL() {
	return this.url.toExternalForm();
    }

    public String getRequestMethod() {
	return this.httpURLConnection.getRequestMethod();
    }

    public String getRequestProperty(String key) {
	return this.httpURLConnection.getRequestProperty(key);
    }

    public void setRequestMethod(String method) throws IOException {
	Assert.isNotNull(method);

	closeCheck();
	nonConnectCheck();

	this.httpURLConnection.setRequestMethod(method);

	if (POST.equals(method)) {
	    this.httpURLConnection.setDoOutput(true);
	} else {
	    this.httpURLConnection.setDoOutput(false);
	}
    }

    public void setRequestProperty(String key, String value) throws IOException {
	Assert.isNotNull(key);
	Assert.isNotNull(value);

	closeCheck();
	nonConnectCheck();

	this.httpURLConnection.setRequestProperty(key, value);
    }

    private void nonConnectCheck() throws IOException {
	if (connect) {
	    throw new IOException("Connected.");
	}
    }

    private void closeCheck() throws IOException {
	if (this.close) {
	    throw new IOException("Closed.");
	}
    }

    private void connectCheck() throws IOException {
	if (!this.connect) {
	    this.httpURLConnection.connect();
	    this.connect = true;
	}
    }

    public InputStream openInputStream() throws IOException {
	closeCheck();
	connectCheck();

	return this.httpURLConnection.getInputStream();
    }

    public OutputStream openOutputStream() throws IOException {
	closeCheck();

	return this.httpURLConnection.getOutputStream();
    }

    public long getDate() throws IOException {
	connectCheck();

	return this.httpURLConnection.getDate();
    }

    public long getExpiration() throws IOException {
	connectCheck();

	return this.httpURLConnection.getExpiration();
    }

    public String getHeaderField(String name) throws IOException {
	connectCheck();

	return this.httpURLConnection.getHeaderField(name);
    }

    public long getLastModified() throws IOException {
	connectCheck();

	return this.httpURLConnection.getLastModified();
    }

    public int getResponseCode() throws IOException {
	connectCheck();

	return this.httpURLConnection.getResponseCode();
    }

    public String getResponseMessage() throws IOException {
	connectCheck();

	return this.httpURLConnection.getResponseMessage();
    }

    public String getType() throws IOException {
	connectCheck();

	return this.httpURLConnection.getContentType();
    }

    public String getEncoding() throws IOException {
	connectCheck();

	return this.httpURLConnection.getContentEncoding();
    }

    public long getLength() throws IOException {
	connectCheck();

	return this.httpURLConnection.getContentLength();
    }

    public void close() throws IOException {
	if (!this.close) {
	    this.close = true;
	    this.httpURLConnection.disconnect();
	}
    }
}
